﻿using Brc_PaaS.Common.Helper;
using Brc_PaaS.Common.Model.Apollo;
using Brc_PaaS.Common.Model.Basic;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Brc_PaaS.Core.Apollo.Service.Implement
{
    public class ApolloNamespaceService : IApolloNamespaceService
    {
        /// <summary>
        /// 获取App的环境，集群信息
        /// </summary>
        /// <param name="conditionDto"></param>
        /// <returns></returns>
        public ResponseModel<List<EnvironmentEnvclusterInfoDto>> GetEnvclusterInfos(BaseRequestDto conditionDto)
        {
            if (conditionDto == null)
            {
                return new ResponseModel<List<EnvironmentEnvclusterInfoDto>>()
                {
                    IsSuccess = false,
                    Msg = "参数对象为空"
                };
            }

            string url = string.Format("http://{0}/openapi/v1/apps/{1}/envclusters", conditionDto.Portal_address, conditionDto.AppId);
            var tokenHeader = GetTokenHeader(conditionDto.Token);
            var strEnvclusterInfoList = HttpHelper.HttpGet(url, tokenHeader);
            var envclusterInfoList = JsonConvert.DeserializeObject<List<EnvironmentEnvclusterInfoDto>>(strEnvclusterInfoList);

            return new ResponseModel<List<EnvironmentEnvclusterInfoDto>>
            {
                IsSuccess = true,
                Data = envclusterInfoList
            };
        }
        /// <summary>
        /// 获取集群下所有Namespace信息接口
        /// </summary>
        /// <param name="conditionDto"></param>
        /// <returns></returns>
        public ResponseModel<List<NamespaceInfoDto>> GetNamespaceInfos(NamespaceConditionDto conditionDto)
        {
            if (conditionDto == null)
            {
                return new ResponseModel<List<NamespaceInfoDto>>
                {
                    IsSuccess = false,
                    Msg = "参数对象为空"
                };
            }

            string url = string.Format(" http://{0}/openapi/v1/envs/{1}/apps/{2}/clusters/{3}/namespaces",
                conditionDto.Portal_address, conditionDto.Env, conditionDto.AppId, conditionDto.ClusterName);
            var tokenHeader = GetTokenHeader(conditionDto.Token);
            var strNamespaceInfoList = HttpHelper.HttpGet(url, tokenHeader);
            var namespaceInfoList = JsonConvert.DeserializeObject<List<NamespaceInfoDto>>(strNamespaceInfoList);

            return new ResponseModel<List<NamespaceInfoDto>>
            {
                IsSuccess = true,
                Data = namespaceInfoList
            };
        }
        /// <summary>
        /// 获取某个Namespace信息接口
        /// </summary>
        /// <param name="conditionDto"></param>
        /// <returns></returns>
        public ResponseModel<NamespaceInfoDto> GetNamespaceInfo(SingleNamespaceConditionDto conditionDto)
        {
            if (conditionDto == null)
            {
                return new ResponseModel<NamespaceInfoDto>
                {
                    IsSuccess = false,
                    Msg = "参数对象为空"
                };
            }

            string url = string.Format("http://{0}/openapi/v1/envs/{1}/apps/{2}/clusters/{3}/namespaces/{4}",
                conditionDto.Portal_address, conditionDto.Env, conditionDto.AppId, conditionDto.ClusterName, conditionDto.NamespaceName);
            var tokenHeader = GetTokenHeader(conditionDto.Token);
            var strNamespaceInfo = HttpHelper.HttpGet(url, tokenHeader);
            var namespaceInfo = JsonConvert.DeserializeObject<NamespaceInfoDto>(strNamespaceInfo);
            return new ResponseModel<NamespaceInfoDto>
            {
                IsSuccess = true,
                Data = namespaceInfo
            };
        }

        /// <summary>
        /// 创建Namespace
        /// </summary>
        /// <param name="createNamespaceRequestDto"></param>
        /// <returns></returns>
        public ResponseModel<CreateNamespaceResultDto> CreateNamespaceInfo(CreateNamespaceDto createNamespaceRequestDto)
        {
            if (createNamespaceRequestDto == null)
            {
                return new ResponseModel<CreateNamespaceResultDto>
                {
                    IsSuccess = false,
                    Msg = "参数对象为空"
                };
            }

            string url = string.Format(" http://{0}/openapi/v1/apps/{1}/appnamespaces", createNamespaceRequestDto.Portal_address, createNamespaceRequestDto.AppId);
            var tokenHeader = GetTokenHeader(createNamespaceRequestDto.Token);
            string postdata = JsonConvert.SerializeObject(createNamespaceRequestDto);
            var strNamespaceInfo = HttpHelper.HttpPost(url, postdata, tokenHeader);
            var namespaceInfoDto = JsonConvert.DeserializeObject<CreateNamespaceResultDto>(strNamespaceInfo);

            if (!string.IsNullOrWhiteSpace(strNamespaceInfo))
            {
                var exceptionInfoDto = JsonConvert.DeserializeObject<ApolloExceptionInfoDto>(strNamespaceInfo);
                if (exceptionInfoDto != null && !string.IsNullOrWhiteSpace(exceptionInfoDto.Message) )
                {
                    return new ResponseModel<CreateNamespaceResultDto>()
                    {
                        Msg = "请求apollo创建匿名空间接口出错：" + exceptionInfoDto.Message,
                        IsSuccess = false
                    };
                    
                }
            }

            return new ResponseModel<CreateNamespaceResultDto>
            {
                IsSuccess = true,
                Data = namespaceInfoDto
            };
        }


        /// <summary>
        ///  获取某个Namespace当前编辑人接口
        /// </summary>
        /// <param name="condtionDto"></param>
        /// <returns></returns>
        public ResponseModel<NamespaceLockInfoDto> GetNamespaceLockInfo(SingleNamespaceConditionDto condtionDto)
        {
            if (condtionDto == null)
            {
                return new ResponseModel<NamespaceLockInfoDto>
                {
                    IsSuccess = false,
                    Msg = "参数对象为空"
                };
            }

            string url = string.Format("http://{0}/openapi/v1/envs/{1}/apps/{2}/clusters/{3}/namespaces/{4}/lock",
                condtionDto.Portal_address, condtionDto.Env, condtionDto.AppId, condtionDto.ClusterName, condtionDto.NamespaceName);
            var tokenHeader = GetTokenHeader(condtionDto.Token);
            var strLockeInfoDto = HttpHelper.HttpGet(url, tokenHeader);
            var lockInfoDto= JsonConvert.DeserializeObject<NamespaceLockInfoDto>(strLockeInfoDto);
            return new ResponseModel<NamespaceLockInfoDto>
            {
                IsSuccess = true,
                Data = lockInfoDto
            };
        }

        public Dictionary<string, string> GetTokenHeader(string token)
        {
            Dictionary<string, string> keyValues = new Dictionary<string, string>();
            keyValues.Add("Authorization", token);
            return keyValues;
        }
    }
}
